pandas之Series对象

您所在的位置:网站首页 series 取值 pandas之Series对象

pandas之Series对象

#pandas之Series对象| 来源: 网络整理| 查看: 265

pandas库的Series对象用来表示一维数据结构,跟数组类似,但多了一些额外的功能,它的内部结构很简单,由两个相互关联的数组组成(index和values),其中主数组用来存放数据,主数组的每一个元素都有一个与之相关联的标签,这些标签存储在一个Index的数组中.

声明Series对象 构造参数如下: def __init__(self, data=None, index=None, dtype=None, name=None, copy=False, fastpath=False): >>> import pandas as pd >>> s = pd.Series([1, 2, 3, 4]) >>>print(s) 0 1 1 2 2 3 3 4 dtype: int64

左侧是index(索引),声明Series时,若不指明index,pandas默认使用从0开始依次递增的数值作为index数组。 右侧是values(即封装的数据).

调用构造函数时指定index选项

>>> s = pd.Series([1, 2, 3,4], index=['a', 'b', 'c', 'd']) >>> print(s) a 1 b 2 c 3 d 4 dtype: int64 使用index和values属性查看组成Series对象的两个数组 >>> print(s.index) Index(['a', 'b', 'c', 'd'], dtype='object') >>> print(type(s.index)) >>> print(s.values) [1 2 3 4] >>> print(type(s.values)) 获取内部数据 直接使用data数组的下标获取: >>> print(s[2]) 3 >>> print(type(s[2]))

通过index数组的值来获取对应的data数组中的值

>>> print(s['c']) 3 获取多个元素 >>>print(s[0:2]) a 1 b 2 dtype: int64 >>> print(type(s[0:2])) >>>print(s[['a', 'b']]) ---通过索引index里面的值来获取相应的元素 a 1 b 2 dtype: int64 为元素赋值: 使用上面介绍的方法选取元素,然后进行赋值. >>> s[1] = 9999 >>> print(s) a 1 b 9999 c 3 d 4 dtype: int64 >>> s['c'] = 666 >>> print(s) a 1 b 9999 c 666 d 4 dtype: int64 使用Numpy数组或者其他的Series对象定义新的Series对象. >>> import numpy as np >>> a = np.array([11, 12, 13, 14]) >>> s2 = pd.Series(a) ---Series对象的values属性本来就是一个Numpy.Array对象 >>> print(s2) --- Series对象中的values数组(数据)是对numpy.ndarray对象的引用,如果改变原有对象的值,Series对象的值也会跟着改变 0 11 1 12 2 13 3 14 dtype: int32 >>> s3 = pd.Series(s) >>> print(s3) a 1 b 9999 c 666 d 4 dtype: int64 筛选元素 因为pandas库是以numpy库为基础开发的,所以numpy数组的许多操作方法对Series对象也有效。 获取所有大于8的元素 >>> print(s[s>8]) b 9999 c 666 dtype: int64 Series对象运算和数学函数 使用与numpy数组的运算符(+,-,*,/)或其他数学函数,也适用于Series对象 >>> print(s/2) a 0.5 b 4999.5 c 333.0 d 2.0 dtype: float64 >>> print(np.log(s)) a 0.000000 b 9.210240 c 6.501290 d 1.386294 dtype: float64 Series对象的组成函数 去除重复的元素,使用Series对象的unique()函数,返回一个numpy.ndarray数组 >>> s = pd.Series([1, 3, 5,3, 7, 9, 7, 5, 4,1, 2]) >>> print(s.unique()) --- 元素的顺序随机 [1 3 5 7 9 4 2] >>>print(type(s.unique()))

Series对象的value_counts()函数返回一个Series对象,index为原Series对象中不重复的元素,values为不重复的元素出现的次数.

>>> print(s.value_counts()) 7 2 5 2 3 2 1 2 9 1 4 1 2 1 dtype: int64

isin()函数可以用来判定Series中的每个元素中是否包含在给定的isin()的参数(数组)中,如果包含在,则为True,否则为False,可用于筛选数据。

>>> s.isin([1,7]) 0 True 1 False 2 False 3 False 4 True 5 False 6 True 7 False 8 False 9 True 10 False dtype: bool >>> s[s.isin([1, 7])] 0 1 4 7 6 7 9 1 dtype: int64 NaN NaN表示Not a Number,pandas.NaN表示这个值,当数据结构中包含的元素为空或者不符合数字的定义时,用这个特定的值来表示,一般来讲,NaN值表示数据有问题,必须对其进行处理. >>> s = pd.Series([3, -1, 15, np.NaN]) >>> print(s) 0 3.0 1 -1.0 2 15.0 3 NaN dtype: float64

isnull()函数和notnull()函数用来识别没有对应元素的索引时非常好用.

>>> s.isnull() 0 False 1 False 2 False 3 True dtype: bool >>> s[s.isnull()] 3 NaN dtype: float64 >>>s.notnull() 0 True 1 True 2 True 3 False dtype: bool Series用作字典 因为Series对象的index对应values,所以我们可以用字典对象来构造Series对象.字典中的所有的键放在Series对象的index数组中,字典中的所有值放在Series对象的values数组中,仍然保持对应关系. >>> dic = {"a": 3, "b": 4, "c": 5} >>> s = pd.Series(dic) >>> print(s) a 3 b 4 c 5 dtype: int64

当然是用字典对象构造Series对象的时候也可以指定index参数,如果index中的值在字典中有对应的键,则生成的Series对象中该值对应的元素为在字典中对应的值,如果找不到,则为pandas.NaN.

>>> s = pd.Series(dic, index=["a","b","c", "d"]) >>> print(s) a 3.0 b 4.0 c 5.0 d NaN --- 字典中不存在"d"这个键 dtype: float64 Series对象之间的运算 Series对象除了和标量之间可以进行运算,Series对象和Series对象之间也可以进行运算.如果index的值没有对齐,则没有对齐的元素运算之后的值为pandas.NaN. >>> s2 = pd.Series({"a": 1, "b": 7, "c": 2, "d": 11}) >>> print(s+s2) a 4.0 b 11.0 c 7.0 d NaN dtype: float64


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3